home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / parted / alignment.py < prev    next >
Encoding:
Python Source  |  2010-06-29  |  5.2 KB  |  120 lines

  1. #
  2. # geometry.py
  3. # Python bindings for libparted (built on top of the _ped Python module).
  4. #
  5. # Copyright (C) 2009 Red Hat, Inc.
  6. #
  7. # This copyrighted material is made available to anyone wishing to use,
  8. # modify, copy, or redistribute it subject to the terms and conditions of
  9. # the GNU General Public License v.2, or (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY expressed or implied, including the implied warranties of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  13. # Public License for more details.  You should have received a copy of the
  14. # GNU General Public License along with this program; if not, write to the
  15. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. # 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
  17. # source code or documentation are not subject to the GNU General Public
  18. # License and may only be used or replicated with the express permission of
  19. # Red Hat, Inc.
  20. #
  21. # Red Hat Author(s): Chris Lumens <clumens@redhat.com>
  22. #                    David Cantrell <dcantrell@redhat.com>
  23. #
  24.  
  25. import parted
  26. import _ped
  27.  
  28. from decorators import localeC
  29.  
  30. class Alignment(object):
  31.     """Alignment()
  32.  
  33.        An Alignment object describes constraints on how sectors and Geometry
  34.        objects are aligned.  Being aligned means that the sector be located at
  35.        a specific sector multiple on a device, or that a geometry must start
  36.        and end at sectors at those specific multiples.  Most methods on this
  37.        object raise ArithmeticError if calculating alignments fails."""
  38.     @localeC
  39.     def __init__(self, *args, **kwargs):
  40.         """Create a new Alignment object from the sectors offset and
  41.            grainSize."""
  42.         if kwargs.has_key("PedAlignment"):
  43.             self.__alignment = kwargs.get("PedAlignment")
  44.         elif kwargs.has_key("offset") and kwargs.has_key("grainSize"):
  45.             self.__alignment = _ped.Alignment(kwargs.get("offset"),
  46.                                               kwargs.get("grainSize"))
  47.         else:
  48.             raise parted.AlignmentException, "no offset+grainSize or PedAlignment specified"
  49.  
  50.     offset = property(lambda s: s.__alignment.offset, lambda s, v: setattr(s.__alignment, "offset", v))
  51.     grainSize = property(lambda s: s.__alignment.grain_size, lambda s, v: setattr(s.__alignment, "grain_size", v))
  52.  
  53.     def __eq__(self, other):
  54.         return not self.__ne__(other)
  55.  
  56.     def __ne__(self, other):
  57.         if hash(self) == hash(other):
  58.             return False
  59.  
  60.         if type(self) != type(other):
  61.             return True
  62.  
  63.         return self.offset != other.offset or self.grainSize != other.grainSize
  64.  
  65.     def __str__(self):
  66.         s = ("parted.Alignment instance --\n"
  67.              "  offset: %(offset)s  grainSize: %(grainSize)s\n"
  68.              "  PedAlignment: %(ped)r" %
  69.              {"offset": self.offset, "grainSize": self.grainSize,
  70.               "ped": self.__alignment})
  71.         return s
  72.  
  73.     @localeC
  74.     def intersect(self, b):
  75.         """Create and return a new Alignment that describes the intersection of
  76.            self and alignment b.  A sector will satisfy the new alignment iff
  77.            it satisfies both of the original alignments.  Whether a sector
  78.            satisfies a given alignment is determined by is_aligned()."""
  79.         return parted.Alignment(PedAlignment=self.__alignment.intersect(b.getPedAlignment()))
  80.  
  81.     @localeC
  82.     def alignUp(self, geom, sector):
  83.         """Return the closest sector to the provided sector that lies inside
  84.            geom and satisfies the alignment constraint self.  This method
  85.            prefers, but does not guarantee, that the result is beyond sector.
  86.            If no such sector can be found, an ArithmeticError is raised."""
  87.         return self.__alignment.align_up(geom.getPedGeometry(), sector)
  88.  
  89.     @localeC
  90.     def alignDown(self, geom, sector):
  91.         """Return the closest sector to the provided sector that lies inside
  92.            geom and satisfies the alignment constraint self.  This method
  93.            prefers, but does not guarantee, that the result is below sector.
  94.            If no such sector can be found, an ArithmeticError is raised."""
  95.         return self.__alignment.align_down(geom.getPedGeometry(), sector)
  96.  
  97.     @localeC
  98.     def alignNearest(self, geom, sector):
  99.         """Return the closest sector to the input sector that lies inside
  100.            geom and satisfies the alignment constraint self.  If no such sector
  101.            can be found, an ArithmeticError is raised."""
  102.         return self.__alignment.align_nearest(geom.getPedGeometry(), sector)
  103.  
  104.     @localeC
  105.     def isAligned(self, geom, sector):
  106.         """Determine whether sector lies inside geom and satisfies the
  107.            alignment constraint self."""
  108.         if not geom:
  109.             raise TypeError, "missing parted.Geometry parameter"
  110.  
  111.         if sector is None:
  112.             raise TypeError, "missing sector parameter"
  113.  
  114.         return self.__alignment.is_aligned(geom.getPedGeometry(), sector)
  115.  
  116.     def getPedAlignment(self):
  117.         """Return the _ped.Alignment object contained in this Alignment.
  118.            For internal module use only."""
  119.         return self.__alignment
  120.